cphsoc_convert_from_spss <- function( spss_file_path,
                                      out_dirname='',
                                      out_filename='',
                                      na='',
                                      source_enc='',
                                      target_enc='',
                                      save_robj=TRUE,
                                      save_csv=TRUE,
                                      save_varlist=TRUE,
                                      save_stata=TRUE,
                                      dta_replace=FALSE
                                    )
{
  # check if any conversion is requested
  if(!any(save_robj,save_csv,save_varlist,save_stata))
    stop('No output requested, please select output formats')
  
  trim <- function(x) {
    gsub('"',"'",gsub('\\s+$|^\\s+','',x))
  }
  
  # check if file exists
  if(!file.exists(spss_file_path))
    stop(paste('File not found: ',spss_file_path))
  
  # split basename into dirname, filename and extension
  in_path_info <- cphsoc_path_info(spss_file_path)
  
  if(nchar(out_dirname)==0) out_dirname <- in_path_info$dirname
  if(nchar(out_filename)==0) out_filename <- in_path_info$filename
  
  ####################################################
  # names and file paths
  ####################################################
  csv_path          <- file.path(out_dirname,paste(out_filename,'.csv',sep=''))
  stata_csv_path    <- file.path(out_dirname,paste(out_filename,'_stata.csv',sep=''))
  stata_do_path     <- file.path(out_dirname,paste(out_filename,'.do',sep=''))
  RData_path        <- file.path(out_dirname,paste(out_filename,'.RData',sep=''))
  varlist_html_path <- file.path(out_dirname,paste(out_filename,'.html',sep=''))
  renamed_vars_path <- file.path(out_dirname,paste(out_filename,'_renamed.txt',sep=''))
  
  # name of R object
  Robj_name <- make.names(out_filename)
  
  # write-paths to do-file, assume files in current directory
  stata_incode_csv_path <- paste(out_filename,'_stata.csv',sep='')
  stata_incode_dta_path <- paste(out_filename,'.dta',sep='')

  ####################################################
  # load spss file, convert to data.frame
  ####################################################
  require(foreign)
  df <- read.spss(spss_file_path,use.value.labels=FALSE,reencode=source_enc)
  class(df) <- "data.frame"
  attr(df,'row.names') <- paste(1:length(df[[1]]))
  
  var_names <- names(df)
  missings   <- attr(df,'missings')
  val_labels <- attr(df,'label.table')
  var_labels <- attr(df,'variable.labels')
  
  # replace backtick with single quote in value labels
  val_labels <- lapply(val_labels,function(z) { if(!is.null(names(z))){names(z) <- gsub("´","'",names(z));z}})
  attr(df,'value.labels') <- val_labels
  
  attr(df,'label.table') <- NULL
  
  num_vars <- length(var_names)
  num_obs <- nrow(df)
  
  output_report <- list()
  
  output_report[[length(output_report)+1]] <- paste("Number of variables: ",num_vars,sep='')
  output_report[[length(output_report)+1]] <- paste("Number of observations: ",num_obs,sep='')
  
  ####################################################
  # save R object
  ####################################################
  if(save_robj)
  {
    assign(Robj_name,df)
    save(list=Robj_name,file=RData_path)
    rm(list=Robj_name)
    output_report[[length(output_report)+1]] <- paste("R data saved as: ",RData_path,sep='')
    output_report[[length(output_report)+1]] <- paste("R data name: ",Robj_name,sep='')
  }
  
  ####################################################
  # save html table of variables and labels
  ####################################################
  if(save_varlist)
  {
    con <- file(varlist_html_path,"w+",encoding=target_enc)
    cphsoc_write_varlist_html(con,var_labels)
    close(con)
    output_report[[length(output_report)+1]] <- paste("Variable list (html file) saved as: ",varlist_html_path,sep='')
  }
  
  ####################################################
  # save csv file (raw data)
  ####################################################
  if(save_csv)
  {
    con <- file(csv_path,"w+",encoding=target_enc)
    write.table(df,file=con,na=na,sep=',',qmethod='escape',col.names=TRUE,row.names=FALSE)
    close(con)
    output_report[[length(output_report)+1]] <- paste("Raw data saved as: ",csv_path,sep='')
  }
  
  if(save_stata)
  {
    res_list <- cphsoc_stata_export(df=df,
                        stata_do_path=stata_do_path,
                        infile_data_path=stata_incode_csv_path,
                        stata_csv_path=stata_csv_path,
                        renamed_vars_path=renamed_vars_path,
                        target_enc=target_enc,
                        stata_dta_path=stata_incode_dta_path,
                        dta_replace=dta_replace)
    output_report <- c(output_report,res_list)
  }
  paste(unlist(output_report))
}